package com.musicapp;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.media.Image;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.transition.Transition;

import java.util.List;

public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ViewHolder> {
    private List<ItemModel> itemList;
    private boolean isAutoPlaying = false;
    private Runnable autoPlayRunnable;
    private Handler handler;
    private int currentPosition = 0;
    private OnItemClickListener mOnItemClickListen;

    public ItemAdapter(List<ItemModel> itemList){
        this.itemList = itemList;
        this.handler = new Handler(Looper.getMainLooper());
        this.autoPlayRunnable = this::autoPlay;
    }

    public interface OnItemClickListener{
        void onItemClick(View view,int position);
    }

    public void setmOnItemClickListen(OnItemClickListener onItemClickListen){
        this.mOnItemClickListen = onItemClickListen;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.banner,parent,false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        ItemModel item = itemList.get(position);
        holder.bind(item);
        if(mOnItemClickListen!=null){
            holder.imageView1.setOnClickListener(v->mOnItemClickListen.onItemClick(v,position));
            holder.addList.setOnClickListener(v -> mOnItemClickListen.onItemClick(v,position));
        }
    }

    @Override
    public int getItemCount() {
        return itemList.size();
    }

    private void autoPlay(){
        currentPosition = (currentPosition + 1) % itemList.size();
        notifyItemChanged(currentPosition);
        handler.postDelayed(autoPlayRunnable,3000);
    }

    public void startAutoPlay(){
        isAutoPlaying = true;
        handler.post(autoPlayRunnable);
    }

//    public void stopAutoPlay() {
//        isAutoPlaying = false;
//        handler.removeCallbacks(autoPlayRunnable);
//    }

    public static class ViewHolder extends RecyclerView.ViewHolder{
        public ImageView imageView1;
        public ImageView addList;
        public ViewHolder(@NonNull View itemview){
            super(itemview);
            imageView1 = itemview.findViewById(R.id.banner);
            addList = itemview.findViewById(R.id.add);
        }
        public void bind (ItemModel itemModel){
            Glide.with(itemView.getContext())
                    .load(itemModel.getCoverImage())
                    .into(imageView1);
        }
    }
}